/*
 * Rights.java
 *
 * Created on 12 wrzesie� 2007, 21:43
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.mikrotubula.db;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * Entity class Rights
 * 
 * @author Krystian
 */
@Entity
@Table(name = "RIGHTS")
@NamedQueries( {
        @NamedQuery(name = "Rights.findByRghId", query = "SELECT r FROM Rights r WHERE r.rghId = :rghId"),
        @NamedQuery(name = "Rights.findByRghName", query = "SELECT r FROM Rights r WHERE r.rghName = :rghName"),
        @NamedQuery(name = "Rights.findAll", query = "SELECT r FROM Rights r")
    })
public class Rights implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "RGH_ID", nullable = false)
    private Integer rghId;

    @Column(name = "RGH_NAME")
    private String rghName;

    @JoinColumn(name = "RGH_GRP_ID", referencedColumnName = "GRP_ID")
    @ManyToOne
    private Groups rghGrpId;
    
    @ManyToMany(mappedBy="rights")
    private Collection<Profiles> profiles;
    
    @Transient
    private Boolean selected;
    
    /** Creates a new instance of Rights */
    public Rights() {
    }

    /**
     * Creates a new instance of Rights with the specified values.
     * @param rghId the rghId of the Rights
     */
    public Rights(Integer rghId) {
        this.rghId = rghId;
    }

    /**
     * Gets the rghId of this Rights.
     * @return the rghId
     */
    public Integer getRghId() {
        return this.rghId;
    }

    /**
     * Sets the rghId of this Rights to the specified value.
     * @param rghId the new rghId
     */
    public void setRghId(Integer rghId) {
        this.rghId = rghId;
    }

    /**
     * Gets the rghName of this Rights.
     * @return the rghName
     */
    public String getRghName() {
        return this.rghName;
    }

    /**
     * Sets the rghName of this Rights to the specified value.
     * @param rghName the new rghName
     */
    public void setRghName(String rghName) {
        this.rghName = rghName;
    }


    /**
     * Returns a hash code value for the object.  This implementation computes 
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.rghId != null ? this.rghId.hashCode() : 0);
        return hash;
    }

    /**
     * Determines whether another object is equal to this Rights.  The result is 
     * <code>true</code> if and only if the argument is not null and is a Rights object that 
     * has the same id field values as this object.
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Rights)) {
            return false;
        }
        Rights other = (Rights)object;
        if (this.rghId != other.rghId && (this.rghId == null || !this.rghId.equals(other.rghId))) return false;
        return true;
    }

    /**
     * Returns a string representation of the object.  This implementation constructs 
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return "info.mikrotubula.db.Rights[rghId=" + rghId + "]";
    }

    public Collection<Profiles> getProfiles() {
        return profiles;
    }

    public void setProfiles(Collection<Profiles> profiles) {
        this.profiles = profiles;
    }

    public Groups getRghGrpId() {
        return rghGrpId;
    }

    public void setRghGrpId(Groups rghGrpId) {
        this.rghGrpId = rghGrpId;
    }

    public Boolean getSelected() {
        if(selected == null) 
            return false;
        else
            return selected;
    }

    public void setSelected(Boolean selected) {
        this.selected = selected;
    }
    
    public void addProfile(Profiles profile) {
        this.getProfiles().add(profile);
    }
    
    public void dropProfile(Profiles profile) {
        this.getProfiles().remove(profile);
    }

}
